package com.whately.core.database.db;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;
import java.util.List;
import java.util.Properties;

@Configuration
@Slf4j
@AutoConfigureOrder(-100)
public class SqlSessionFactoryConfig {

    /******************************************************************
     * Mybatis多数据源配置类
     *
     *******************************************************************/


    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dynamicDataSource) throws Exception {
        log.info("**** load sqlSessionFactory begin ");
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        //        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dynamicDataSource);
        // 设置mybatis的主配置文件
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //设置主配置文件
        Resource mybatisConfigXml = resolver.getResource("classpath:mybatis/mybatis-config.xml");
        sqlSessionFactoryBean.setConfigLocation(mybatisConfigXml);

        //此处设置为了解决找不到mapper文件的问题
        //Resource[] resourceArray1 = resolver.getResources("mybatis/mapper/*.xml");
        //Resource[] resourceArray2 = resolver.getResources("mybatis/mapper/pub/*.xml");
        //Resource[] resources = ArrayUtils.addAll(resourceArray1,resourceArray2);

        //这个写法已经包含了目录和子目录
        Resource[] resources = resolver.getResources("classpath*:mybatis/mapper/**/*.xml");

        //把work Flowable 的表添加进来
        try{
            Resource flowableUiResource = resolver.getResource("classpath:/META-INF/modeler-mybatis-mappings");
            if(flowableUiResource!=null&&flowableUiResource.exists()){
                Resource[] resourcesForFlowable = resolver.getResources("classpath:/META-INF/modeler-mybatis-mappings/*.xml");
                if(resourcesForFlowable!=null&&resourcesForFlowable.length>0){
                    if(resources==null||resources.length<=0){
                        resources = resourcesForFlowable;
                    }else{
                        List<Resource> ls= Lists.newArrayList();
                        for(Resource r:resources){
                            ls.add(r);
                        }
                        for(Resource r:resourcesForFlowable){
                            ls.add(r);
                        }

                        resources = ls.toArray(new Resource[ls.size()]);
                    }

                    Properties properties = new Properties();
                    properties.put("prefix", "");
                    properties.put("blobType", "BLOB");
                    properties.put("boolValue", "TRUE");
                    sqlSessionFactoryBean.setConfigurationProperties(properties);

                }
            }

        }catch(java.io.FileNotFoundException e){
            log.warn("FileNotFoundException modeler-mybatis-mappings");
        }


        sqlSessionFactoryBean.setMapperLocations(resources);
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
